package com.cps116.friendstracker;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.easy.facebook.android.apicall.FQL;
import com.easy.facebook.android.error.EasyFacebookError;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class QueryActivity_MyCheckinsActivity extends BaseActivity 
{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queryactivity_mycheckinsactivity_layout);

		ArrayList<String> myCheckins = new ArrayList<String>();
		FQL facebookFQL = ((FriendsTrackerApplication) getApplication()).getFacebookFQL();

		try {
			String myCheckinsXML = facebookFQL.fqlQuery("SELECT name " +
														"FROM place " +
														"WHERE page_id IN (SELECT page_id " +
																		  "FROM checkin " +
																		  "WHERE author_uid = me())");

			Pattern p = Pattern.compile("<name>(.+?)</name>");
			Matcher m = p.matcher(myCheckinsXML);

			while(m.find())
			{
				myCheckins.add(m.group(1));
			}		
		} catch (EasyFacebookError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String[] result;
		
		if(myCheckins.isEmpty()) 
		{
			result = new String[1];
			result[0] = "No Check-ins";
		}
		else {

			result = new String[myCheckins.size()];
			
			for(int i = 0; i < myCheckins.size(); i++)
			{
				result[i] = myCheckins.get(i);
			}
		}

		ListView list=(ListView)findViewById(R.id.queryactivity_mycheckinactivity_listview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result);
		list.setAdapter(adapter);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
